using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Xeml.Sampling.Contracts;
using Xeml.Document;
using Xeml.Sampling;

namespace Xid.Controls
{
    public partial class SampleProviderCtrl : UserControl
    {

        private DataProviderResource sampleRes;

        public DataProviderResource SampleProviderRes
        {
            get { return sampleRes; }
            set 
            { 
                sampleRes = value;
                if (sampleRes != null && sampleRes.ComponentState == ComponentLoadState.Loaded)
                {
                    this.provider = sampleRes.Provider;
                    this.provider.AuthenticationRequest += new EventHandler<Xeml.Sampling.AuthenticationRequestEventArgs>(OnProviderAuthenticationRequest);
                }
            }
        }

        void provider_AuthenticationRequest(object sender, Xeml.Sampling.AuthenticationRequestEventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        private IDataProvider provider;

        //public ISampleProvider Provider
        //{
        //    get { return provider; }
        //    set 
        //    { 
        //        provider = value;
        //        if(provider != null)
        //            provider.AuthenticationRequest += new EventHandler<Xeml.Sampling.AuthenticationRequestEventArgs>(OnProviderAuthenticationRequest);
        //    }
        //}

        void OnProviderAuthenticationRequest(object sender, Xeml.Sampling.AuthenticationRequestEventArgs e)
        {
            AuthRequestForm arf = new AuthRequestForm();
            arf.AuthArgs = e;
           // arf.ProviderName = provider.FriendlyName;
            arf.ShowDialog();
           
        }

        [Bindable(true, BindingDirection.TwoWay)]
        public string ConnectionString
        {
            get { return textBoxConnectionString.Text; }
            set { textBoxConnectionString.Text = value; }
        }

        [Bindable(true, BindingDirection.TwoWay)]
        public string FriendlyName
        {
            get { return textBoxFriendlyName.Text; }
            set { textBoxFriendlyName.Text = value; }
        }

        public string Publisher
        {
            get { return this.textBoxPublisher.Text; }
            set { this.textBoxPublisher.Text = value; }
        }

        public string CompId
        {
            get { return this.textBoxCompId.Text; }
            set { this.textBoxCompId.Text = value; }
        }

        public string Version
        {
            get { return this.textBoxVersion.Text; }
            set { this.textBoxVersion.Text = value; }
        }


        public SampleProviderCtrl()
        {
            InitializeComponent();
            textBoxConnectionString.TextChanged += new EventHandler(textBoxConnectionString_TextChanged);
            textBoxFriendlyName.TextChanged += new EventHandler(textBoxFriendlyName_TextChanged);
        }

        void textBoxFriendlyName_TextChanged(object sender, EventArgs e)
        {
            if (FriendlyNameChanged != null)
                FriendlyNameChanged(this, new EventArgs());

            labelDispName.Text = textBoxFriendlyName.Text;
        }

        void textBoxConnectionString_TextChanged(object sender, EventArgs e)
        {
            if (ConnectionStringChanged != null)
                ConnectionStringChanged(this, new EventArgs());
        }

        public event EventHandler ConnectionStringChanged;
        public event EventHandler FriendlyNameChanged;


        private bool showDetails = true;

        public bool ShowDetails
        {
            get { return showDetails; }
            set 
            {
                if (showDetails != value)
                {
                    showDetails = value;
                    if (showDetails)
                    {
                        pictureBox.Size = new Size(64, 76);
                        this.Size = new Size(300, 170);
                        this.tableLayoutPanel1.Enabled = true;
                        this.tableLayoutPanel1.Visible = true;
                        this.BorderStyle = BorderStyle.FixedSingle;
                        labelDispName.Visible = false;
                    }
                    else
                    {
                        labelDispName.Visible = true;
                        pictureBox.Size = new Size(140,170);
                        this.Size = new Size(170, 170);
                        
                        this.tableLayoutPanel1.Enabled = false;
                        this.tableLayoutPanel1.Visible = false;
                        this.BorderStyle = BorderStyle.None;
                    }
                }
            }
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            ShowDetails = !ShowDetails;
        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            //ConnectResult cr = this.provider.TestConnection();
            //if (cr == ConnectResult.Established)
            //    MessageBox.Show("Connection test passed", "Connection test", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //else if(cr == ConnectResult.Failed)
            //    MessageBox.Show("Connection test failed", "Connection test", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
